package com.woniuxy.carlife.carlifeuserweb.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.carlife.carlifecommonsweb.exception.CarlifeGlobalException;
import com.woniuxy.carlife.carlifecommonsweb.util.CopyUtil;
import com.woniuxy.carlife.carlifeusermodel.dto.FavoritesDto;
import com.woniuxy.carlife.carlifeusermodel.dto.ServiceInfofavoritesDto;
import com.woniuxy.carlife.carlifeusermodel.param.FavoritesParam;
import com.woniuxy.carlife.carlifeusermodel.param.Prodectfavoritesparam;
import com.woniuxy.carlife.carlifeuserweb.entity.*;
import com.woniuxy.carlife.carlifeuserweb.mapper.ProdectfavoritesMapper;
import com.woniuxy.carlife.carlifeuserweb.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zjq
 * @ClassName:ProdectfavoritesServiceImpl
 * @Description:
 * @date 2021/4/6 20:02
 **/

@Service
public class ProdectfavoritesServiceImpl extends ServiceImpl<ProdectfavoritesMapper, Prodectfavorites> implements ProdectfavoritesService {

    @Resource
    ProdectfavoritesMapper prodectfavoritesMapper;

    @Resource
    StorefavoritesService storefavoritesService;

    @Resource
    ServiceInfofavoritesService serviceInfofavoritesService;

    @Resource
    ChargingStationfavoritesService chargingStationfavoritesService;

    @Resource
    StatisticsService statisticsService;

    /*
     * 功能描述〈根据用户id查询收藏的商品〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 13:16
     * @throws
     */

    @Override
    public List<Prodectfavorites> findProdectfavoritesByUserId(Integer userId) {
        QueryWrapper<Prodectfavorites> prodectfavoritesQueryWrapper = new QueryWrapper<>();
        prodectfavoritesQueryWrapper.eq("user_id", userId);
        List<Prodectfavorites> prodectfavorites = prodectfavoritesMapper.selectList(prodectfavoritesQueryWrapper);

        return prodectfavorites == null ? null : prodectfavorites;
    }


    /*
     * 功能描述〈查询所有收藏〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 14:44
     * @throws
     */

    @Override
    public List<FavoritesDto> findFavoritesByUserId(Integer userId) {

//        获取商品的
        List<Prodectfavorites> prodectfavoritesByUserId = findProdectfavoritesByUserId(userId);

        List<FavoritesDto> favoritesList = CopyUtil.getList(prodectfavoritesByUserId, FavoritesDto.class);

//        获取门店
        List<Storefavorites> storefavorites = storefavoritesService.findStorefavorites(userId);

        for (Storefavorites storefavorite : storefavorites) {
            FavoritesDto one = CopyUtil.getOne(storefavorite, FavoritesDto.class);
            favoritesList.add(one);
        }

//        获取门店服务的

        List<ServiceInfofavorites> serviceInfofavoritesByUserId = serviceInfofavoritesService.findServiceInfofavoritesByUserId(userId);
        for (ServiceInfofavorites serviceInfofavorites : serviceInfofavoritesByUserId) {
            FavoritesDto one = CopyUtil.getOne(serviceInfofavoritesByUserId, FavoritesDto.class);
            favoritesList.add(one);
        }


//        获取充电桩的;
        List<ChargingStationfavorites> chargingStationfavoritesByUserId = chargingStationfavoritesService.findChargingStationfavoritesByUserId(userId);
        for (ChargingStationfavorites chargingStationfavorites : chargingStationfavoritesByUserId) {
            FavoritesDto one = CopyUtil.getOne(chargingStationfavorites, FavoritesDto.class);
            favoritesList.add(one);
        }


        return favoritesList == null ? null : favoritesList;

    }


    /*
     * 功能描述〈删除商品收藏〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/14 14:29
     * @throws
     */

    @Override
    public boolean deleteProdectfavoritesByProductfavoritesId(Integer productfavoritesId) {
        int i = prodectfavoritesMapper.deleteById(productfavoritesId);

        return i > 0 ? true : false;
    }

    /*
     * 功能描述〈删除收藏〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/14 15:21
     * @throws
     */


    @Override
    public boolean deleteFavorites(FavoritesParam favoritesParam, Integer tokenUserId) {
//        删除商品收藏
        if (ObjectUtil.isNotNull(favoritesParam.getProductfavoritesId())) {
            boolean b = deleteProdectfavoritesByProductfavoritesId(favoritesParam.getProductfavoritesId());
            if (b) {
                boolean b1 = statisticsService.reduceFavorites(tokenUserId);
                return b1 ? true : false;
            }

        }
//        删除门店收藏
        if (ObjectUtil.isNotNull(favoritesParam.getStorefavoritesId())) {
            Boolean aBoolean = storefavoritesService.deleteStorefavoritesByStorefavoritesId(favoritesParam.getStorefavoritesId());
            if (aBoolean) {
                boolean b1 = statisticsService.reduceFavorites(tokenUserId);
                return b1 ? true : false;
            }

        }
//        删除门店服务收藏
        if (ObjectUtil.isNotNull(favoritesParam.getTServiceInfofavoritesId())) {
            Boolean aBoolean = serviceInfofavoritesService.deleteiceInfofavoritesByUserIdtServiceInfofavoritesId(favoritesParam.getTServiceInfofavoritesId());
            if (aBoolean) {
                boolean b1 = statisticsService.reduceFavorites(tokenUserId);
                return b1 ? true : false;
            }

        }

//        删除充电站收藏
        if (ObjectUtil.isNotNull(favoritesParam.getTChargingStationfavoritesId())) {
            Boolean aBoolean = chargingStationfavoritesService.deleteChargingStationfavoritesBytChargingStationfavoritesId(favoritesParam.getTChargingStationfavoritesId());
            if (aBoolean) {
                boolean b1 = statisticsService.reduceFavorites(tokenUserId);
                return b1 ? true : false;
            }

        }

        return false;
    }


    /*
     * 功能描述〈据用户id新增收藏的商品〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/14 18:43
     * @throws
     */

    @Override
    public Boolean addProdectfavoritesByUserid(Prodectfavorites prodectfavorites) {

        int insert = prodectfavoritesMapper.insert(prodectfavorites);
        if (insert>0){
            boolean b = statisticsService.modifyFavorites(prodectfavorites.getUserId());
            if (b){
                return true;
            }
        }
        return false;
    }

    /*
     * 功能描述〈统计商品收藏数量〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/15 16:46
     * @throws
     */

    @Override
    public Integer prodectfavoritesTotalByUserId(Integer userId) {
        QueryWrapper<Prodectfavorites> shoppingcartQueryWrapper = new QueryWrapper<>();
        // QueryWrapper<Employee> queryWrapper2=Wrappers.<Employee>query();
        shoppingcartQueryWrapper.eq("user_id",userId);
        Integer count = prodectfavoritesMapper.selectCount(shoppingcartQueryWrapper);
        return count;

    }


}
